import 'package:cloud_medicine/components/shop_grade.dart';
import 'package:cloud_medicine/http/config.dart';
import 'package:cloud_medicine/http/shop_api.dart';
import 'package:cloud_medicine/pages/order_list.dart';
import 'package:flutter/material.dart';

class ShopGrade extends StatefulWidget {
  final num orderid;
  ShopGrade({Key key, this.orderid}) : super(key: key);

  @override
  _ShopGradeState createState() => _ShopGradeState();
}

class _ShopGradeState extends State<ShopGrade> {
  List<Map> _listmap = [
    {'selectValue': 'selectValue1', 'gradeitem': '总评'},
    {'selectValue': 'selectValue2', 'gradeitem': '服务'},
    {'selectValue': 'selectValue3', 'gradeitem': '药品'},
  ];
  List<num> _grade = [5, 5, 5];
  Future _addShopGrade() async {
    num grade;
    String i;
    print(_grade);
    grade = (_grade[0] + _grade[1] + _grade[2]) / 3;
    i = grade.toStringAsFixed(2);
    grade = double.parse(i);
    print(grade);
    await addShopGrade_api(
        '$backIP/shop/shopGrade', {"ID": widget.orderid, "grade": grade});
  }

  Future _changeStatus(num id, num status) async {
    await editOrderStatus_api(
        '$backIP/shop/orderstatus', {'current_status': status, 'ID': id});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[300],
      appBar: AppBar(
        title: Text('我要评价'),
      ),
      body: ListView(
        children: [
          Container(
              margin: EdgeInsets.only(left: 50),
              padding: EdgeInsets.all(10),
              child: Column(
                children: _listmap.map((e) {
                  return ShopGradeItem(
                    gradeitem: e['gradeitem'],
                    selectValue: e['selectValue'],
                    grade: (value) {
                      _grade[_listmap.indexOf(e)] = value;
                    },
                  );
                }).toList(),
              ))
        ],
      ),
      floatingActionButton: Container(
        margin: EdgeInsets.only(left: 35),
        width: double.infinity,
        height: 40,
        decoration: BoxDecoration(
            gradient: LinearGradient(
                colors: [Colors.lightBlueAccent, Colors.blueAccent]),
            borderRadius: BorderRadius.circular(20)),
        child: RaisedButton(
          onPressed: () {
            _addShopGrade();
            _changeStatus(widget.orderid, 3);
            Navigator.of(context).push(MaterialPageRoute(
                builder: (BuildContext context) => OrderList()));
          },
          child: Text(
            '提交',
            style: TextStyle(color: Colors.white, fontWeight: FontWeight.w100),
          ),
          color: Colors.transparent,
          elevation: 0,
          highlightElevation: 0,
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
        ),
      ),
    );
  }
}
